package cn.gbase.jiangsu.data.transfer.service;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.ShopUser;
import cn.gbase.jiangsu.data.transfer.common.Constants;
import cn.gbase.jiangsu.data.transfer.util.CommonUtil;

@IocBean(args = { "refer:dao" })
public class CommunityUserService extends IdEntityService<ShopUser> {

	@Inject
	private BuildingService buildingService;

	public CommunityUserService() {
		super();
	}

	public CommunityUserService(Dao dao) {
		super(dao);
	}

	/**
	 * 根据ID取得用户.
	 * 
	 * @param userId
	 *            用户ID
	 * @return
	 */
	public ShopUser getCommunityUser(int userId) {
		return this.dao().fetch(ShopUser.class, userId);
	}

	/**
	 * 取得用户列表.
	 * 
	 * @param pageNumber
	 * @param name
	 * @return
	 */
	public QueryResult getUserList(int pageNumber, String communityId,
			String buildingNo, String unitNo, String roomNo, String username) {

		if (pageNumber <= 0) {
			pageNumber = 1;
		}

		// 拼接条件
		SqlExpressionGroup group = Cnd.exps("1", "=", 1);
		if (communityId != null && communityId.length() > 0) {
			group = group.and("communityId", "=", Integer.valueOf(communityId));
		}
		if (buildingNo != null && buildingNo.length() > 0) {
			group = group.and("buildingNo", "=", buildingNo);
		}
		if (unitNo != null && unitNo.length() > 0) {
			group = group.and("unitNo", "=", unitNo);
		}
		if (roomNo != null && roomNo.length() > 0) {
			group = group.and("roomNo", "=", roomNo);
		}
		if (username != null && username.length() > 0) {
			group = group.and("username", "=", username);
		}

		// 最大页数
		Pager pager = this.dao().createPager(pageNumber,
				Constants.LIST_ONE_PAGE_SIZE);

		// 查询记录总数
		int recordCount = this.dao().count(ShopUser.class, Cnd.where(group));
		pager.setRecordCount(recordCount);

		// 没数据，直接返回
		if (recordCount == 0) {
			return new QueryResult(new ArrayList<ShopUser>(0), pager);
		}

		// 计算页数
		int maxPageNumber = 0;
		if (recordCount % Constants.LIST_ONE_PAGE_SIZE == 0) {
			maxPageNumber = recordCount / Constants.LIST_ONE_PAGE_SIZE;
		} else {
			maxPageNumber = recordCount / Constants.LIST_ONE_PAGE_SIZE + 1;
		}
		if (pageNumber > maxPageNumber) {
			pager.setPageNumber(maxPageNumber);
		}

		// 查询
		List<ShopUser> resultList = this.dao().query(ShopUser.class,
				Cnd.where(group), pager); // 当前页面显示记录

		// 返回
		return new QueryResult(resultList, pager);
	}

	/**
	 * 删除用户.
	 * 
	 * @param userId
	 */
	public void deleteUser(int userId) {
		this.dao().delete(ShopUser.class, userId); // 根据ID删除
	}

	/**
	 * 修改密码.
	 * 
	 * @param id
	 *            用户ID
	 * @param newPW
	 *            新密码
	 */
	public void modifyPW(int id, String newPW) {
		String encodePW = CommonUtil.encodePw(newPW);
		this.dao().update(ShopUser.class, Chain.make("pw", encodePW),
				Cnd.where("id", "=", id));
	}

	/**
	 * 取得商城登录用户的地址.
	 * 
	 * @return 地址
	 */
	public String getShopLoginUserAddress() {

		int userId = CommonUtil.getShopLoginUserId();
		ShopUser user = this.getCommunityUser(userId);
		return buildingService.getRoomAddress(user.getRoomId());
	}

}
